// Fill out your copyright notice in the Description page of Project Settings.


#include "AnimPlayer/Actions/TranslateAction.h"
#include "CurveWrappers/FloatCurveWrapper.h"

void FTranslateAction::OnInit()
{
	FActionBase::OnInit();
	if (Layer.IsValid() && CurveWrapper.IsValid())
	{
		const float Value = CurveWrapper->GetValueByTime(0);
		const FVector DirectionVec = GetDirectionVector(Direction);
		Layer->DoTranslate(AnimTarget,ElementId, DirectionVec * Value);
	}
}

void FTranslateAction::OnBegin()
{
	FActionBase::OnBegin();
	if (Layer.IsValid() && CurveWrapper.IsValid())
	{
		const float Value = CurveWrapper->GetValueByTime(0);
		const FVector DirectionVec = GetDirectionVector(Direction);
		Layer->DoTranslate(AnimTarget, ElementId, DirectionVec * Value);
	}
}

void FTranslateAction::OnTick(float DeltaTime)
{
	FActionBase::OnTick(DeltaTime);
	if (Layer.IsValid() && CurveWrapper.IsValid())
	{
		const float Value = CurveWrapper->GetValueByTime(Timer);
		const FVector DirectionVec = GetDirectionVector(Direction);
		Layer->DoTranslate(AnimTarget, ElementId, DirectionVec * Value);
	}
}

void FTranslateAction::OnEnd()
{
	FActionBase::OnEnd();
	if (Layer.IsValid() && CurveWrapper.IsValid())
	{
		const float Value = CurveWrapper->GetValueByTime(Duration);
		const FVector DirectionVec = GetDirectionVector(Direction);
		Layer->DoTranslate(AnimTarget, ElementId, DirectionVec * Value);
	}
}

FVector FTranslateAction::GetDirectionVector(const EAnimDirection& AnimDirection)
{
	FVector Result(0, 0, 0);
	switch (AnimDirection)
	{
	case EAnimDirection::Up:
		Result = FVector(0, 0, 1);
		break;
	case EAnimDirection::Down:
		Result = FVector(0, 0, -1);
		break;
	case EAnimDirection::Left:
		Result = FVector(0, -1, 0);
		break;
	case EAnimDirection::Right:
		Result = FVector(0, 1, 0);
		break;
	default:
		break;
	}
	return Result;
}
